//
//  RootViewController.m
//  eBaySearch
//
//  Created by Dang Duc Nam (namk42c@yahoo.com) on 4/29/09.
//  Copyright 2009 Unitech. All rights reserved.
//

#import "RootViewController.h"
#import "XMLAppDelegate.h"
#import "eBayItem.h"
#import "ItemViewController.h"
static UIImage *initImage = nil;
@implementation RootViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [appDelegate.list count];
}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
	
	CGRect CellFrame = CGRectMake(0, 0, 310, 60);
	CGRect Label1Frame = CGRectMake(0, 0, 270, 30);
	CGRect Label2Frame = CGRectMake(0, 35, 100, 20);
	CGRect ImageFrame = CGRectMake(270, 0, 50, 55);
	
	UIImageView *itemImageView;
	UILabel *lblTemp;
	
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
	
	//Initialize Label with tag 1.
	lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
	lblTemp.tag = 1;
	lblTemp.font = [UIFont boldSystemFontOfSize:12];
	lblTemp.numberOfLines = 0;
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	//Initialize Label with tag 2.
	lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
	lblTemp.tag = 2;
	lblTemp.font = [UIFont boldSystemFontOfSize:10];
	lblTemp.textColor = [UIColor lightGrayColor];
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];

	//Initialize Label with tag 3.
	itemImageView = [[UIImageView alloc] initWithFrame:ImageFrame];
	itemImageView.tag = 3;
	[cell.contentView addSubview:itemImageView];
	[cell.contentView bringSubviewToFront:itemImageView];
	
	return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if(cell == nil)
		cell = [self getCellContentView:CellIdentifier];
	
	UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
	UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
	UIImageView *imageView = (UIImageView *)[cell viewWithTag:3];

	//First get the item object
	eBayItem *item = [appDelegate.list objectAtIndex:indexPath.row];
		
	lblTemp1.text = item.title;
	lblTemp2.text = item.price;

	imageView.image = item.image;
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller
	
	if(idvController == nil)
		idvController = [[ItemViewController alloc] initWithNibName:@"ItemView" bundle:[NSBundle mainBundle]];
	else { //already have a view? release it and create new, for time being
		[idvController release];
		idvController = [[ItemViewController alloc] initWithNibName:@"ItemView" bundle:[NSBundle mainBundle]];
	}

	eBayItem *anItem = [appDelegate.list objectAtIndex:indexPath.row];
	idvController.item = anItem;
	
	[self.navigationController pushViewController:idvController animated:YES];
}

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
	
	self.tableView.scrollEnabled = NO;
	
	//Add the done button.
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
											   initWithBarButtonSystemItem:UIBarButtonSystemItemDone
											   target:self action:@selector(doneSearching_Clicked:)] autorelease];
}

- (void) doneSearching_Clicked:(id)sender {
	[appDelegate FindItems:searchBar.text];
	self.title = [NSString stringWithFormat:@"Searching : %@",searchBar.text];
	searchBar.text = @"";
	[searchBar resignFirstResponder];
	
	self.navigationItem.rightBarButtonItem = nil;
	self.tableView.scrollEnabled = YES;
	[self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to add the Edit button to the navigation bar.
	initImage = [[UIImage imageNamed:@"2.0.png"] retain];

	appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
	//Add the search bar
	self.tableView.tableHeaderView = searchBar;
	searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
	self.title = @"EBay Search";
}



/*
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[idvController release];
	[appDelegate release];
    [super dealloc];
}


@end

